﻿using System;
using System.ComponentModel.DataAnnotations;

namespace MyLiveMesh.Web.Models
{
    public class RegisterData
    {
        [Key]
        [Required()]
        [RegularExpression("^[a-zA-Z0-9_]*$", ErrorMessage = "Invalid user name. It must contain only alphanumeric characters")]
        public string UserName { get; set; }

        [Key]
        [Required()]
        [RegularExpression(@"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$", ErrorMessage = "Invalid email. An email must use the format username@mycompany.com")]
        public string Email { get; set; }

        [Required()]
        [RegularExpression("^.*[^a-zA-Z0-9].*$", ErrorMessage = "A password needs to contain at least one special character e.g. @ or #")]
        [StringLength(50, MinimumLength = 7, ErrorMessage = "Invalid password. It must be contain at least 7 characters and no more than 50 characters.")]
        public string Password { get; set; }

        [Required()]
        [CustomValidation(typeof(RegistrationValidator), "IsPasswordConfirmed")]
        public string ConfirmPassword { get; set; }

        [Required()]
        public string SecurityQuestion { get; set; }

        [Required()]
        public string SecurityAnswer { get; set; }
    }

    public class RegistrationValidator
    {
        public static ValidationResult IsPasswordConfirmed(string confirmPassword, ValidationContext context)
        {
            RegisterData data = context.ObjectInstance as RegisterData;
            if (data.Password == confirmPassword)
            {
                return ValidationResult.Success;
            }
            else
            {
                return new ValidationResult("Please confirm your password by providing it again.");
            }
        }
    }
}